//
//  AppDelegate.swift
//  OdmIM
//
//  Created by Gao on 17/1/16.
//  Copyright © 2017年 OdmIM. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //设置用户信息提供者为自己 AppDelegate
        RCIM.shared().userInfoDataSource = self
        
//        // 测试连接融云服务器
//        connectRongServer {}
        
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

// 实现融云代理
extension AppDelegate: RCIMUserInfoDataSource {
    
    func getUserInfo(withUserId userId: String!, completion: ((RCUserInfo?) -> Void)!) {
        let userInfo = RCUserInfo()
        userInfo.userId = userId
        
        // -----------------测试帐号-------------------------
        //userid:guyanqiu
        //name:guyanqiu
        //portraitUri:http://img3.imgtn.bdimg.com/it/u=214931719,1608091472&fm=21&gp=0.jpg
        //{"code":200,"userId":"guyanqiu","token":"IeODWt94pGEOTb+l1O+9aFJsM8sWxg1LHIvDjv5UFRbUbUT7wuqlDFpdCGHvi7pC3Cps8FxIvzb9Lip4D33GP9CQeXZh3A9t"}
        
        //userid:gaoh
        //name:gaoh
        //portraitUri:http://img4.imgtn.bdimg.com/it/u=1368451630,1525355250&fm=11&gp=0.jpg
        //{"code":200,"userId":"gaoh","token":"xny2f4KREZoZDIvbmi489VJsM8sWxg1LHIvDjv5UFRbUbUT7wuqlDOOikWGDUTgQ5GG87uKlWrIUINhHVvBKew=="}
        
        switch userId {
        case "guyanqiu":
            userInfo.name = "guyanqiu"
            userInfo.portraitUri = "http://img3.imgtn.bdimg.com/it/u=214931719,1608091472&fm=21&gp=0.jpg"
        case "gaoh":
            userInfo.name = "gaoh"
            userInfo.portraitUri = "http://img4.imgtn.bdimg.com/it/u=1368451630,1525355250&fm=11&gp=0.jpg"
        default:
            print("无此用户")
        }
        // -----------------测试帐号-------------------------
        
        
        return completion(userInfo)        
    }
    
}

// 连接融云服务器
extension AppDelegate {

    
    func connectRongServer(_ completion:@escaping ()->Void) {
        //初始化app key
        RCIM.shared().initWithAppKey(RongAppKey)
    
        // 默认以 用户“gaoh” 连接融云服务器
        RCIM.shared().connect(withToken: "xny2f4KREZoZDIvbmi489VJsM8sWxg1LHIvDjv5UFRbUbUT7wuqlDOOikWGDUTgQ5GG87uKlWrIUINhHVvBKew==",
                              success: { (str) in
                                let currentUserInfo = RCUserInfo(userId: "gaoh", name: "gaoh", portrait: "http://img3.imgtn.bdimg.com/it/u=214931719,1608091472&fm=21&gp=0.jpg")
//                                let currentUserInfo = RCUserInfo(userId: "guyanqiu", name: "guyanqiu", portrait: "http://img3.imgtn.bdimg.com/it/u=214931719,1608091472&fm=21&gp=0.jpg")
                                RCIMClient.shared().currentUserInfo = currentUserInfo
                                print("连接成功!")
                                
                                DispatchQueue.main.async(execute: { () -> Void in
                                    completion()
                                })
                              },
                              error: { (code:RCConnectErrorCode) -> Void in
                                print("无法连接！\(code)")
                              })
                              {
                                print("无效token！")
                              }
    }
    
}





























